package com.hubu.parking.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hubu.parking.entity.park.ParkingRecord;
import com.hubu.parking.entity.park.ParkingSpot;
import com.hubu.parking.entity.park.User;
import com.hubu.parking.service.ParkingRecordService;
import com.hubu.parking.service.ParkingSpotService;
import com.hubu.parking.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@SpringBootTest(classes = com.hubu.parking.ParkingApplication.class)
public class testSpot {

    @Resource
    private ParkingSpotService parkingSpotService;

    @Resource
    private ParkingRecordService parkingRecordService;

    @Resource
    private UserService userService;

    @Test
    public void addSpotData() {
        String first = "A";
        String spilt = "-";
        for (int i = 1; i <= 50; i++) {
            ParkingSpot parkingSpot = new ParkingSpot();
            String spotNumber = first + spilt + i;
            parkingSpot.setSpotNumber(spotNumber);
            parkingSpot.setLocation(String.valueOf(RandomUtil.randomInt(20, 400)));
            parkingSpot.setStatus(RandomUtil.randomInt(0, 3));
            parkingSpotService.saveOrUpdate(parkingSpot);
        }
        String second = "B";
        for (int i = 1; i <= 50; i++) {
            ParkingSpot parkingSpot = new ParkingSpot();
            String spotNumber = second + spilt + i;
            parkingSpot.setSpotNumber(spotNumber);
            parkingSpot.setLocation(String.valueOf(RandomUtil.randomInt(20, 400)));
            parkingSpot.setStatus(RandomUtil.randomInt(0, 3));
            parkingSpotService.saveOrUpdate(parkingSpot);
        }
    }

    @Test
    public void addParkingRecord() {
        parkingRecordService.remove(new LambdaQueryWrapper<>());
        List<User> users = userService.list();
        List<ParkingSpot> spots = parkingSpotService.list();
        for (int i = 0; i < 450; i++) {
            ParkingRecord record = new ParkingRecord();
            record.setUserId(users.get(RandomUtil.randomInt(0, users.size())).getId());
            record.setSpotId(spots.get(RandomUtil.randomInt(0, spots.size())).getId());
            Date startDate = randomDateStr();
            record.setStartTime(startDate);
            record.setEndTime(getFarTime(startDate));
            record.setFee(RandomUtil.randomBigDecimal(new BigDecimal(0), new BigDecimal(1000)));
            parkingRecordService.save(record);
        }
    }

    private static Date randomDateStr() {
        DateTime start = DateUtil.parse("2025-01-01 ");
        DateTime randomDate = RandomUtil.randomDate(start, DateField.SECOND, 0, 600000);
        long time = randomDate.getTime();
        return new Date(time);
    }

    private static Date getFarTime(Date start) {
        long startTime = start.getTime();
        long end = startTime + RandomUtil.randomLong(1000L, 1000 * 60 * 60 * 24 * 8L);
        return new Date(end);
    }

    private static String formatTime(Date date) {
        return DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
    }
}
